﻿using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TaggingToolkit.Framework;

namespace TaggingToolkit.Controls
{
    public class ColoredTextRendererAction : HighlightTextRenderer
    {
        internal const TextFormatFlags NormalTextFormatFlags = TextFormatFlags.NoPrefix |
                                                       TextFormatFlags.EndEllipsis |
                                                       TextFormatFlags.PreserveGraphicsTranslateTransform;
        public ColoredTextRendererAction()
        {
        }


        private bool ColumnIsPrimary
        {
            get { return this.Column != null && this.Column.Index == 0; }
        }

        #region Rendering

        /// <summary>
        /// Draw text using GDI
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdi(Graphics g, Rectangle r, string txt)
        {
            Color backColor = Color.Transparent;
            if (this.IsDrawBackground && this.IsItemSelected && ColumnIsPrimary && !this.ListView.FullRowSelect)
                backColor = this.GetSelectedBackgroundColor();

            TextFormatFlags flags = NormalTextFormatFlags | this.CellVerticalAlignmentAsTextFormatFlag;

            // I think there is a bug in the TextRenderer. Setting or not setting SingleLine doesn't make 
            // any difference -- it is always single line.
            if (!this.CanWrap)
                flags |= TextFormatFlags.SingleLine;
            TextRenderer.DrawText(g, txt, this.Font, r, GetColorFromAction(), backColor, flags);
        }

        /// <summary>
        /// Draw the text using GDI+
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdiPlus(Graphics g, Rectangle r, string txt)
        {
            //if (this.ShouldDrawHighlighting)
            //    this.DrawGdiPlusTextHighlighting(g, r, txt);

            using (StringFormat fmt = this.StringFormatForGdiPlus)
            {
                // Draw the background of the text as selected, if it's the primary column
                // and it's selected and it's not in FullRowSelect mode.
                Font f = this.Font;
                if (this.IsDrawBackground && this.IsItemSelected && this.ColumnIsPrimary && !this.ListView.FullRowSelect)
                {
                    SizeF size = g.MeasureString(txt, f, r.Width, fmt);
                    Rectangle r2 = r;
                    r2.Width = (int)size.Width + 1;
                    using (Brush brush = new SolidBrush(this.GetSelectedBackgroundColor()))
                    {
                        g.FillRectangle(brush, r2);
                    }
                }
                RectangleF rf = r;
                using (Brush brush = new SolidBrush(GetColorFromAction()))
                {
                    g.DrawString(txt, f, brush, rf, fmt);
                }
            }
        }

        private Color GetColorFromAction()
        {
            TagEditAction action = ((IActionItem)this.ListItem.RowObject).Action;
            switch (action)
            {

                case TagEditAction.Rename:
                    return Color.DarkGoldenrod;
                case TagEditAction.AddBack:
                case TagEditAction.AddFront:
                    return Color.DarkGreen;
                case TagEditAction.Remove:
                    return Color.DarkRed;
                case TagEditAction.Keep:
                default:
                    return GetForegroundColor();
            }
        }

        #endregion
    }
}
